package controller;


import POJO.AppCategory;
import POJO.AppInfo;
import POJO.DataDictionary;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import service.AppCategoryService;
import service.AppInfoService;
import service.DataDictionaryService;
import util.Page;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;


@Controller
@RequestMapping("/app")
public class AppInfoController {

    @Resource
    private DataDictionaryService dataDictionaryService;
    @Resource
    private AppCategoryService appCategoryService;
    @Resource
    private AppInfoService appInfoService;

    @RequestMapping("/list")
    public String appInfoList( AppInfo appInfo,@RequestParam(value = "pageIndex",required = false) Integer pageIndex, Model model){
        System.out.print(appInfo.getSoftwareName());
        //所属平台
        List<DataDictionary> flatformNameList=null;
        //App状态
        List<DataDictionary> statusList=null;
        //App信息
        List<AppInfo> appInfoList=null;
        //1级分类
        List<AppCategory> categoryLevel1List=null;
        //2级分类
        List<AppCategory> categoryLevel2List=null;
        //3级分类
        List<AppCategory> categoryLevel3List=null;

        //页面容量
        int pageSize = 10;
        //当前页码
        Integer currentPageNo = 1;
        if(pageIndex!=null&&pageIndex>0){

            currentPageNo=pageIndex;
        }

        int totalCount=appInfoService.totalCountAppInfo(appInfo);
        Page page=new Page();
        page.setPageSize(pageSize);
        page.setCurrPageNo(currentPageNo);
        page.setTotalCount(totalCount);
        if(currentPageNo < 1){
            currentPageNo = 1;
        }else if(currentPageNo > page.getTotalPageCount()){
            currentPageNo = page.getTotalPageCount();
        }
        int pageNo = (page.getCurrPageNo() - 1) * pageSize;
        appInfoList=appInfoService.selAllOrByCondition(appInfo,pageNo,pageSize);
        flatformNameList=dataDictionaryService.getDataDictionaryByTypeCode("APP_FLATFORM");
        statusList=dataDictionaryService.getDataDictionaryByTypeCode("APP_STATUS");
        categoryLevel1List = appCategoryService.getAppCategoryByPrintId(null);
        model.addAttribute("appList",appInfoList);
        model.addAttribute("page",page);
        model.addAttribute("flatformNameList",flatformNameList);
        model.addAttribute("statusList",statusList);
        model.addAttribute("categoryLevel1List",categoryLevel1List);

        return "indexss";
    }


    @RequestMapping("categorize")
    @ResponseBody
    public String selectCategorize(@RequestParam(value="parentId") Integer parentId){

        List<AppCategory> list=appCategoryService.getAppCategoryByPrintId(parentId);

        System.out.print(JSON.toJSONString(list));
return JSON.toJSONString(list);


    }


    @RequestMapping("/listss")
    @ResponseBody
    public String appInfoListss( AppInfo appInfo,@RequestParam(value = "pageIndex",required = false) Integer pageIndex, Model model){

        //App信息
        List<AppInfo> appInfoList=null;

        //页面容量
        int pageSize = 10;
        //当前页码
        Integer currentPageNo = 1;
        if(pageIndex!=null&&pageIndex>0){

            currentPageNo=pageIndex;
        }

        int totalCount=appInfoService.totalCountAppInfo(appInfo);
        Page page=new Page();
        page.setPageSize(pageSize);
        page.setCurrPageNo(currentPageNo);
        page.setTotalCount(totalCount);
        if(currentPageNo < 1){
            currentPageNo = 1;
        }else if(currentPageNo > page.getTotalPageCount()){
            currentPageNo = page.getTotalPageCount();
        }
        int pageNo = (page.getCurrPageNo() - 1) * pageSize;
        appInfoList=appInfoService.selAllOrByCondition(appInfo,pageNo,pageSize);
        HashMap<String ,Object> resultMap=new   HashMap<String ,Object>();
        resultMap.put("appList",appInfoList);
        resultMap.put("page",page);

        System.out.print(resultMap);
        return JSON.toJSONString(resultMap);
    }


    //添加APP基础信息
    @RequestMapping("/AddAppInfo")
    @ResponseBody
    public int AddAppInfo(AppInfo appInfo) {

        String thps = appInfo.getLogoPicPath();
        int addindex=appInfoService.AddAppInfo(appInfo);

        return addindex;
    }
    //根据ID查询App信息
    @RequestMapping("/AppInfoId")
    public String AppInfoId(Integer noid, Model model){
        if(noid==null){
            model.addAttribute("staff","AddAppInfo");
            return "indexss";
        }if(noid==-1){
            model.addAttribute("staff","index");
            return "indexss";
        }
        AppInfo appInfo=new AppInfo();
        appInfo=appInfoService.appInfoListId(noid);
        model.addAttribute("appInfo",appInfo);
        model.addAttribute("staff","update");
        //   model.addAttribute("status",appInfoService.DataDictionaryList("APP_STATUS"));
        //  model.addAttribute("appCategorylist",appInfoService.appCategoryList(null));
        //  model.addAttribute("flatforms",appInfoService.DataDictionaryList("APP_FLATFORM"));
        //if判断不能改变位置
        if(noid==null){
            model.addAttribute("staff","AddAppInfo");
            return "indexss";
        }if(noid==-1){
            model.addAttribute("staff","index");
            return "indexss";
        }

        model.addAttribute("getCategoryLevel2",appCategoryService.getAppCategoryByPrintId(appInfo.getCategoryLevel1()));
        model.addAttribute("getCategoryLevel3",appCategoryService.getAppCategoryByPrintId(appInfo.getCategoryLevel2()));

        return "indexss";
    }
    //添加APP基础信息
    @RequestMapping("/updateAppInfo")
    @ResponseBody
    public int updateAppInfo(AppInfo appInfo,@RequestParam(value = "appinfoid", required = false) Integer appinfoid) {
        int status= appInfo.getStatus();
        //判断App状态id如果审核未通过，改为待审核状态id
        if(appinfoid==0){
            if(status==3){
                appInfo.setStatus(1);
            }}
        int addindex=appInfoService.UpdateAppInfo(appInfo);
        return addindex;
    }

    @RequestMapping("/AppInfoList")
    @ResponseBody
    public String appInfoList(@RequestParam(value = "pageNo", required = false) Integer pageNo, AppInfo appInfo
    ) {
        HashMap<String, Object> appInfosMap = new HashMap<String, Object>();

        Page pages = new Page();
        //总数量（表）
        int totalCount = appInfoService.totalCountAppInfo(appInfo);

        //设置页面容量
        int pageSize = 5;
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        //  int totalPageCount = pages.getTotalPageCount();
        int pageNos = 0;
        if (pageNo == null) {
            pageNo = 1;
        }
        pageNos = (pageNo - 1) * pageSize;
        pages.setCurrPageNo(pageNos);
        List<AppInfo> AppInfoList = appInfoService.selAllOrByCondition(appInfo, pageNos, pageSize);
        appInfosMap.put("AppInfoList", AppInfoList);
        appInfosMap.put("pages", pages);

        String appInfoLists = JSONArray.toJSONString(appInfosMap);
System.out.print(appInfoLists);
        return appInfoLists;
    }


    //删除基础信息表信息
    @RequestMapping("/deleteAppInfo")
    @ResponseBody
    public int deleteAppInfo(int inId) {

        int deteid = appInfoService.deleteAppInfo(inId);
        return deteid;
    }
    @RequestMapping("/ AppInfoLowerShelf")
    @ResponseBody

    public int AppInfoLowerShelf(int inId) {
        int UpdateLowerShelf = appInfoService.AppInfoLowerShelf(inId);

        return UpdateLowerShelf;
    }
}
